package com.service.Impl;

import com.dao.HousingEstateMapper;
import com.utils.DataTables;
import com.entity.HousingEstate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.BaseService;
import com.service.HousingEstateService;
import com.utils.RequestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HousingEstateServiceImpl extends BaseService implements HousingEstateService {
    @Autowired
    private HousingEstateMapper housingEstateMapper;

    @Override
    public long getHousingEstateRowCount() {
        return housingEstateMapper.getHousingEstateRowCount();
    }

    /**
     * 查询所有小区信息
     *
     * @return
     */
    @Override
    public List<HousingEstate> selectHousingEstate() {
        return housingEstateMapper.selectHousingEstate();
    }

    /**
     * 1. 获取分页小区列表
     *
     * @param dataTables
     * @return
     */
    @Override
    public DataTables getPageList(DataTables dataTables) {
        //分页开始的页数,每页的长度
        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength()); // 核心分页代码
        //PageHelper.orderBy("sort asc");

        //判断字符是否为空
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(HousingEstateMapper.class, dataTables.getColumn());

            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        PageInfo<HousingEstate> pageInfo = new PageInfo<HousingEstate>
                (housingEstateMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ?
                pageInfo.getList() : new ArrayList<HousingEstate>());

        return dataTables;
    }

    /**
     * 2.添加小区信息
     *
     * @param housingEstate
     * @return
     */
    @Override
    public Map<String, Object> addHousingEstate(HousingEstate housingEstate) {
        //创建map集合
        Map<String, Object> map = new HashMap<String, Object>();
        //添加数据获取添加信息,判断是否添加成功 1成功0失败
        int id = housingEstateMapper.insertHousingEstate(housingEstate);

        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        } else {
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    /**
     * 3.修改小区信息
     *
     * @param housingEstate
     * @return
     */
    @Override
    public Map<String, Object> updateHousingEstate(HousingEstate housingEstate) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (housingEstateMapper.updateNonEmptyHousingEstateById(housingEstate) > 0) {
            map.put("status", RequestStatus.SUCCESS);
        } else {
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    /**
     * 4.删除小区信息
     *
     * @param idList
     * @return
     */
    @Override
    public Map<String, Object> delHousingEstate(List<Integer> idList) {
        Map<String, Object> map = new HashMap<String, Object>();

        int i = housingEstateMapper.deleteByPrimaryKeys(idList);
        if (i > 0) {
            map.put("status", RequestStatus.SUCCESS);
        } else {
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    /**
     * 通过属性查询小区信息
     *
     * @param obj
     * @return
     */
    @Override
    public HousingEstate selectHousingEstateByObj(HousingEstate obj) {
        return housingEstateMapper.selectHousingEstateByObj(obj);
    }

    /**
     * 添加小区信息 包括属性null
     *
     * @param id
     * @return
     */
    //获取小区详细信息
    @Override
    public HousingEstate selectHousingEstateById(Integer id) {
        return housingEstateMapper.selectHousingEstateById(id);
    }


    @Override
    public int insertHousingEstate(HousingEstate value) {
        return housingEstateMapper.insertHousingEstate(value);
    }


    @Override
    public int insertNonEmptyHousingEstate(HousingEstate value) {
        return housingEstateMapper.insertNonEmptyHousingEstate(value);
    }

    //批量添加小区
    @Override
    public int insertHousingEstateByBatch(List<HousingEstate> value) {
        return housingEstateMapper.insertHousingEstateByBatch(value);
    }

    @Override
    public int deleteHousingEstateById(Integer id) {
        return housingEstateMapper.deleteHousingEstateById(id);
    }


    @Override
    public int updateHousingEstateById(HousingEstate enti) {
        return housingEstateMapper.updateHousingEstateById(enti);
    }

    @Override
    public int updateNonEmptyHousingEstateById(HousingEstate enti) {
        return housingEstateMapper.updateNonEmptyHousingEstateById(enti);
    }

    public HousingEstateMapper getHousingEstateMapper() {
        return this.housingEstateMapper;
    }

    public void setHousingEstateMapper(HousingEstateMapper housingEstateMapper) {
        this.housingEstateMapper = housingEstateMapper;
    }



    @Override
    public HousingEstate selectHousingEstateByCode(String housCode) {
        return housingEstateMapper.selectHousingEstateByCode(housCode);
    }



    @Override
    public String getPrivinceByCode(String province_code) {
        return housingEstateMapper.getPrivinceByCode(province_code);
    }

    @Override
    public String getCityByCode(String city_code) {
        return housingEstateMapper.getCityByCode(city_code);
    }

}